package com.abe.mvpcore.use;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.abe.mvpcore.BasePresenter;
import com.abe.mvpcore.PresenterHelper;
import com.abe.mvpcore.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Ex2Activity extends Activity implements ExContract.ExView, ExContract.Ex2View {

    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ex2);
        textView = findViewById(R.id.item_text);
        findViewById(R.id.item_btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((ExPresenter) getPresenter(ExPresenter.class)).reqEx();
            }
        });
        findViewById(R.id.item_btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Ex2Presenter) getPresenter(Ex2Presenter.class)).reqEx2();
            }
        });
        findViewById(R.id.item_btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Ex2Activity.this, Ex3Activity.class));
            }
        });
    }

    private BasePresenter getPresenter(Class<?> presenterClass) {
        return (BasePresenter) PresenterHelper.getInstance().getPresenter(presenterClass, Ex2Activity.this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        PresenterHelper.getInstance().destroy(Ex2Activity.this);
    }

    @Override
    public void onReqEx(boolean isSuccess) {
        if (isSuccess) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss.zzz", Locale.CHINA);
                    String string = "通过MVP改变：" + format.format(new Date());
                    textView.setText(string);
                }
            });
        }
    }

    @Override
    public void onReqEx2(boolean isSuccess) {
        if (isSuccess) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss.zzz", Locale.CHINA);
                    String string = "通过MVP-2改变：" + format.format(new Date());
                    textView.setText(string);
                }
            });
        }
    }

    @Override
    public void onFinish(Object type) {

    }

    @Override
    public void onFailure(Object type) {

    }
}